package com.carkeeper.mongo.service.impl;

import com.carkeeper.appraise.feign.AppraisalFeign;
import com.carkeeper.appraise.pojo.Appraisal;
import com.carkeeper.entity.PageResult;
import com.carkeeper.mongo.config.RabbitMQConfig;
import com.carkeeper.mongo.service.MongoService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MongoServiceImpl implements MongoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private AppraisalFeign appraisalFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void saveAll() {
        List<Appraisal> list = appraisalFeign.findAll().getData();
        for (Appraisal appraisal : list) {
            com.carkeeper.mongo.pojo.Appraisal appraisal1 = new com.carkeeper.mongo.pojo.Appraisal();
            BeanUtils.copyProperties(appraisal,appraisal1);
            mongoTemplate.save(appraisal1);
        }
    }

    @Override
    public com.carkeeper.mongo.pojo.Appraisal findOne(String orderId) {
        Query query = new Query(Criteria.where("orderId").is(orderId));
        com.carkeeper.mongo.pojo.Appraisal appraisal = mongoTemplate.findOne(query, com.carkeeper.mongo.pojo.Appraisal.class);
        return appraisal;
    }

    @Override
    public void delete(String orderId) {
        mongoTemplate.remove(findOne(orderId));
        rabbitTemplate.convertAndSend("", RabbitMQConfig.DEL_APPRAISAL_QUEUE,orderId);
    }

    @Override
    public PageResult<com.carkeeper.mongo.pojo.Appraisal> search(Map<String,Object> searchMap, Integer currPage, Integer pageSize) {
        PageResult<com.carkeeper.mongo.pojo.Appraisal> pageResult = new PageResult<>();
        Query query = new Query();
        if(searchMap!=null&&searchMap.size()>0){
            if(searchMap.containsKey("startTime")||searchMap.containsKey("endTime")){
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date startTime = format.parse(searchMap.get("startTime").toString());
                    Date endTime = format.parse(searchMap.get("endTime").toString());
                    query.addCriteria( Criteria.where("orderCreateTime").gt(startTime).lt(endTime));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            searchMap.remove("startTime");
            searchMap.remove("endTime");
            if(searchMap!=null&&searchMap.size()>0){
            for (String key : searchMap.keySet()) {
                query.addCriteria( Criteria.where(key).is(searchMap.get(key)));
           }
            }

        }
        long count = mongoTemplate.count(query, com.carkeeper.mongo.pojo.Appraisal.class);
        pageResult.setTotal(count);
        Pageable pageable = new PageRequest(currPage-1,pageSize);
        List<com.carkeeper.mongo.pojo.Appraisal> list = mongoTemplate.find(query.with(pageable), com.carkeeper.mongo.pojo.Appraisal.class);
        pageResult.setRows(list);
        return pageResult;
    }

    @Override
    public PageResult<com.carkeeper.mongo.pojo.Appraisal> find(Integer currPage, Integer pageSize) {
        return  search(null,currPage,pageSize);
    }

    @Override
    public void add(Appraisal appraisal) {
        com.carkeeper.mongo.pojo.Appraisal appraisal1 = new com.carkeeper.mongo.pojo.Appraisal();
        BeanUtils.copyProperties(appraisal,appraisal1);
        mongoTemplate.save(appraisal1);
    }
}
